package creationalPatterns.prototypePattern;

import java.util.Hashtable;

public abstract class Shape implements Cloneable {
    private String id;

    protected String type;

    abstract void draw();
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    public Object clone() {
        Object clone = null;
        try{
            clone = super.clone();
        } catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return clone;
    }
}

class Ractangle extends Shape{
    public Ractangle(){
        this.type = "Rectangle";
    }

    @Override
    void draw() {
        System.out.println("Ractanglee:draw()");
    }
}

class Square extends Shape{
    public Square(){
        this.type = "Square";
    }

    @Override
    void draw() {
        System.out.println("Square:draw()");
    }
}

class Circle extends Shape{
    public Circle(){
        this.type = "Circle";
    }

    @Override
    void draw() {
        System.out.println("Circle:draw()");
    }
}

class ShapeCache {
   private static Hashtable<String,Shape> shapeMap = new Hashtable<String,Shape>();
    public static Shape getShape(String shapeId) {
        Shape cachedShape = shapeMap.get(shapeId);
        return (Shape) cachedShape.clone();
    }

    public static void loadCache() {
        Circle circle = new Circle();
        circle.setId("1");
        shapeMap.put(circle.getId(),circle);

        Square square = new Square();
        square.setId("2");
        shapeMap.put(square.getId(),square);

        Ractangle ractangle = new Ractangle();
        ractangle.setId("3");
        shapeMap.put(ractangle.getId(),ractangle);
    }
}